using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesAdminChangeClaimStatusDetail : System.Web.UI.Page
{
    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected DataView ResultView = null;
    protected DataView ResultViewHistory = null;
    protected GonzalesClaimHeader curHeader = null;
    protected GonzalesClaimHeader ResultObj = null;
    protected DataTable ResultHistory = null;
    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState["ResultTable"];
        }

        set
        {
            ViewState["ResultTable"] = value;
        }
    }
    #endregion


    #region UnexpectedError
    protected string UnexpectedError
    {
        get
        {
            return ViewState["UnexpectedError"].ToString();
        }

        set
        {
            ViewState["UnexpectedError"] = value;
        }
    }
    #endregion

    #region Remark
    protected string RemarkMessage
    {
        get
        {
            return ViewState["RemarkMessage"].ToString();
        }

        set
        {
            ViewState["RemarkMessage"] = value;
        }
    }
    #endregion

    #region GridViewHeader
    protected ArrayList GridViewHeader
    {
        get
        {
            return (ArrayList)ViewState["GridViewHeader"];
        }

        set
        {
            ViewState["GridViewHeader"] = value;
        }
    }
    #endregion

    #region ClaimInternalID
    protected string ClaimInternalID
    {
        get
        {
            return ViewState["ClaimInternalID"].ToString();
        }

        set
        {
            ViewState["ClaimInternalID"] = value;
        }
    }
    #endregion

    #region StatusMessage
    protected string StatusMessage
    {
        get
        {
            return ViewState["StatusMessage"].ToString();
        }

        set
        {
            ViewState["StatusMessage"] = value;
        }
    }
    #endregion

    #region SavedFailedMessage
    protected string SavedFailedMessage
    {
        get
        {
            return ViewState["SavedFailedMessage"].ToString();
        }

        set
        {
            ViewState["SavedFailedMessage"] = value;
        }
    }
      #endregion
    #region SavedFailedMessage(Same Status)
    protected string SavedFailedMessageSameStatus
    {
        get
        {
            return ViewState["SavedFailedMessageSameStatus"].ToString();
        }

        set
        {
            ViewState["SavedFailedMessageSameStatus"] = value;
        }
    }
      #endregion
    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        this.Table_MessageBox.Visible = false;  // always default to false
    
        if (!IsPostBack)
        {
            #region Request.QueryString["id"]
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
            {
                this.ClaimInternalID = Request.QueryString["id"];
            }
            else
            {
                Response.Redirect(GonzalesErrorPageRedirect.PageFlowError);
            }
            #endregion

            Label_Personal.Text = "";
            this.DropDownList_Personal.Visible = false;

            this.PageComponentSetup();
            this.SwitchLanguage();
            LoadData();
            this.LoadHistory();
             if (this.ResultTable != null)
             {
                this.BindData(this.ResultView);
             }
             if (this.ResultHistory  != null)
             {
                 this.BindDataHistory(this.ResultViewHistory);
             }
        }
    }
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        bool valid = true;
        StringBuilder ErrorMessageBuilder = new StringBuilder("<ul>");

        //update by mei chze on 18072012
        bool smooth = false;
        ChangeStatus changeDetail = null;

        #region Date Validation

        if (this.TextBox_Remark.Text.Length == 0)
        {
            ErrorMessageBuilder.AppendLine("<li>" + this.RemarkMessage + "</li>");
            valid = false;
        }

        if (this.DropDownList_Status.SelectedIndex == 0)
        {
            ErrorMessageBuilder.AppendLine("<li>" + this.RemarkMessage + "</li>");
            valid = false;
        }
        ErrorMessageBuilder.AppendLine("</ul>");
        #endregion

        // update the status
        if (valid)
        {
            #region find claim header
            changeDetail = new ChangeStatus();
            changeDetail.remark = this.TextBox_Remark.Text;
            changeDetail.ClaimID = this.ClaimInternalID;
            changeDetail.changeOriginStatus = ClaimManager.GetClaimStatusFromCode(Convert.ToString(GetClaimStatusInCode(this.HiddenField_originStatus.Value.Replace(" ", ""))));
            changeDetail.changeToStatus = ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString());
            changeDetail.ChangeDate = DateTime.Now;
            changeDetail.ChangeBy = this.CurrentPrivateSession.CurrentLoggedOnUser;
            #endregion
            //insert change detail 
            using (ChangeManager curChangeManager = new ChangeManager(this.CurrentPrivateSession))
            {

                //Update claim header 
                using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
                {

                    ResultObj = CurClaimManager.GetClaimByInternalID(this.ClaimInternalID);
                    ResultObj.LastUpdatedBy = this.CurrentPrivateSession.CurrentLoggedOnUser;
                    ResultObj.LastUpdatedOn = DateTime.Now;
                    Company comp = null;
                    using (CompanyManager companyManager= new CompanyManager(this.CurrentPrivateSession))
                    {
                        comp = companyManager.GetCompanyByID(ResultObj.CompanyClaimedFrom.InternalID);
                    }
                    using (UserManager curUserManager = new UserManager(this.CurrentPrivateSession))
                    {
                        SystemUser currentClaimUser = new SystemUser();

                        #region select status
                        # region pending to approver

                        if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.Submitted)
                        {
                            #region show the approver
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.Submitted)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                currentClaimUser = curUserManager.GetUserByInternalID(this.HiddenField_personalID.Value.ToString());

                                SystemUser Approver = null;

                                if (currentClaimUser.AppRoute == ApprovingRoute.Normal)
                                {
                                    Approver = currentClaimUser.PersonnelReportTo;
                                }
                                else if (currentClaimUser.AppRoute == ApprovingRoute.MultiLevel)
                                {
                                    Approver = currentClaimUser.PersonnelReportTo;
                                }
                                else if (currentClaimUser.AppRoute == ApprovingRoute.Exception)
                                {
                                    using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                    {
                                        SystemUsers Approvers = CurUserManager.GetExceptionalApproversMatrix(currentClaimUser.InternalID );
                                        if (Approvers != null && Approvers.Count > 0)
                                        {
                                            Approver = Approvers[0];
                                        }
                                    }
                                }

                                if (Approver != null)
                                {
                                    ResultObj.NextApprover = Approver;
                                    smooth = CurClaimManager.UpdateStatusChangeToSumitted(ResultObj);
                                    ////log the action
                                    //// this is to monitor entire claim flow
                                    //this.CurLogManager.LogAction(this.ClaimInternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID, GonzalesUserAction.ClaimApproved, this.ResultObj.Status);
                                    ////Added By Agnes [16th Aug, 2011]
                                    //int count = CurLogManager.GetClaimActionLogCountByClaimID(this.ClaimInternalID);
                                    //if (count == 0)
                                    //    CurLogManager.CreateLogApproverClaimAction(this.ClaimInternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName, ClaimManager.GetClaimStatusInString(this.ResultObj.Status));
                                    //else
                                    //    CurLogManager.UpdateLogApproverClaimAction(this.ClaimInternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName, ClaimManager.GetClaimStatusInString(this.ResultObj.Status), count.ToString());

                        
                                    if (!smooth)
                                    {
                                        ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                        this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateStatusChangeToSumitted(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                        this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                    }
                                    else
                                    {
                                        if (CheckBox_email.Checked)
                                        {
                                            if (this.ResultObj.ClaimedBy.EmailID.ToLower().CompareTo("brian.king") != 0)
                                            {
                                                //this.CurLogManager.LogAction(this.ClaimInternalID, this.CurrentPrivateSession.ActualLoginUser.InternalID, GonzalesUserAction.ClaimSubmitted, GonzalesClaimHeader.TypeOfClaimStatus.Submitted);

                                                #region Send email

                                                if (GonzalesGeneralVariables.EmailActive)
                                                {
                                                    string MailContent = "", MailTitle = "", ReceiverID = "";

                                                    if (this.ResultObj.NextApprover != null)
                                                    {
                                                        using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimSubmitted, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                        {
                                                            MailContent = CurMailGenerator.GenerateSubmittedClaimForApprover(
                                                                this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName,
                                                                this.CurrentPrivateSession.CurrentLoggedOnUser.EmployeeID,
                                                                this.CurrentPrivateSession.CurrentLoggedOnUser.EmailID,
                                                                this.CurrentPrivateSession.CurrentLoggedOnUser.FromCompany.CompanyName,
                                                                this.ResultObj.ClaimID,
                                                                this.ResultObj.Reasons,
                                                                this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                DateTime.Now.ToLongDateString(),
                                                                GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.SUBMITTED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + this.ResultObj.NextApprover.EmailID);

                                                            MailTitle = CurMailGenerator.MailTitle;
                                                            ReceiverID = this.ResultObj.NextApprover.InternalID;
                                                        }
                                                    }
                                                    else if (this.ResultObj.NextAuthorizer != null)
                                                    {
                                                        using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimSubmitted, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                        {
                                                            MailContent = CurMailGenerator.GenerateSubmittedClaimForApprover(
                                                                this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName,
                                                                this.CurrentPrivateSession.CurrentLoggedOnUser.EmployeeID,
                                                                this.CurrentPrivateSession.CurrentLoggedOnUser.EmailID,
                                                                this.CurrentPrivateSession.CurrentLoggedOnUser.FromCompany.CompanyName,
                                                                this.ResultObj.ClaimID,
                                                                this.ResultObj.Reasons,
                                                                this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                DateTime.Now.ToLongDateString(),
                                                                GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.SUBMITTED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + this.ResultObj.NextAuthorizer.EmailID);

                                                            MailTitle = CurMailGenerator.MailTitle;
                                                            ReceiverID = this.ResultObj.NextAuthorizer.InternalID;
                                                        }
                                                    }

                                                    if (MailContent.Length > 0)
                                                    {
                                                        SystemUser Receiver = null;

                                                        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                        {
                                                            Receiver = CurUserManager.GetUserByInternalID(ReceiverID);
                                                        }

                                                        if (Receiver != null)
                                                        {
                                                            using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                Receiver.DisplayName,
                                                                Receiver.Email,
                                                                MailTitle,
                                                                MailContent))
                                                            {
                                                                if (Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                                {
                                                                    if (CurEmailFactory.IsReady)
                                                                    {
                                                                        if (!CurEmailFactory.SendNow())
                                                                        {
                                                                            string LogID = SwissArmy.UniqueID();
                                                                            this.CurLogManager.LogError(LogID, "[GonzalesMyDraftDetail.Button_Submit_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        string LogID = SwissArmy.UniqueID();
                                                                        this.CurLogManager.LogError(LogID, "[GonzalesMyDraftDetail.Button_Submit_Click] : Failed at CurEmailFactory Constructor : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                    }
                                                                }

                                                                //Added by Agnes to send a copy of email to luikta.
                                                                if (Receiver.EmailID.ToLower().Trim().CompareTo("paul.mauerman") == 0)
                                                                {
                                                                    using (EmailFactory NewEmailFactory = new EmailFactory(
                                                                        Receiver.DisplayName,
                                                                        "Lukita.Utari@covidien.com",
                                                                        MailTitle,
                                                                        MailContent))
                                                                    {
                                                                        if (CurEmailFactory.IsReady)
                                                                        {

                                                                            if (!NewEmailFactory.SendNow())
                                                                            {
                                                                                string LogID = SwissArmy.UniqueID();
                                                                                this.CurLogManager.LogError(LogID, "[GonzalesMyDraftDetail.Button_Submit_Click] : Failed at CurEmailFactory.SendNow() : " + NewEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                #endregion

                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>No Approver set up.</li></ul>");
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                            //}
                            

                            #endregion
                        }

                        #endregion

                        # region pending to higher approver
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.Escalated)
                        {
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.Escalated)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                SystemUser Approver = null;
                                Approver = curUserManager.GetUserByInternalID(this.DropDownList_Personal.SelectedValue.ToString());
                                if (Approver != null)
                                {
                                    ResultObj.NextApprover = Approver;
                                    //set the escalate date on getdate()
                                    ResultObj.ProjectedOn = DateTime.Now;
                                }
                                smooth = CurClaimManager.UpdateWhenStatusChangeToEscalated(ResultObj);
                                if (!smooth)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToEscalated(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                                else
                                {
                                    if (CheckBox_email.Checked)
                                    {
                                        if (this.ResultObj.ClaimedBy.EmailID.ToLower().CompareTo("brian.king") != 0)
                                        {
                                            #region Send email

                                            if (GonzalesGeneralVariables.EmailActive)
                                            {
                                                string MailContent = "", MailTitle = "";

                                                #region If the claim need to go to next approver

                                                if (this.ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.Escalated)
                                                {
                                                    SystemUser Receiver = null;
                                                    using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                    {
                                                        Receiver = CurUserManager.GetUserByInternalID(this.ResultObj.NextApprover.InternalID);
                                                    }
                                                    if (Receiver != null && Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                    {
                                                        using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimEscalated, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                        {
                                                            MailContent = CurMailGenerator.GeneratePendingForAuthorizerClaimForAuthorizer(
                                                                this.ResultObj.ClaimedBy.DisplayName,
                                                                this.ResultObj.ClaimedBy.EmployeeID,
                                                                this.ResultObj.ClaimedBy.EmailID,
                                                                comp.CompanyName,
                                                                this.ResultObj.ClaimID,
                                                                this.ResultObj.Reasons,
                                                                this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                this.ResultObj.SubmittedOn.ToLongDateString(),
                                                                GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.ESCALATED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID);

                                                            MailTitle = CurMailGenerator.MailTitle;
                                                        }

                                                        if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                        {
                                                            using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                Receiver.DisplayName,
                                                                Receiver.Email,
                                                                MailTitle,
                                                                MailContent))
                                                            {
                                                                if (!CurEmailFactory.SendNow())
                                                                {
                                                                    string LogID = SwissArmy.UniqueID();
                                                                    this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Approve_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                #endregion
                                            }

                                            #endregion
                                        }
                                    }
                                }
                            //}
                        }

                        #endregion

                        # region pending to PendingforAuthorizer
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize)
                        {

                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.Submitted)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                                else
                                {
                                    SystemUser Claimant = null;
                                    Claimant = curUserManager.GetUserByInternalID(this.HiddenField_personalID.Value.ToString());
                                    if (Claimant.NoAuthorizer)
                                    {
                                        // no authorizer required, so is auto
                                        // now, it is ready for finance verification
                                        ErrorMessageBuilder.AppendLine("<ul><li>No Authorized Needed.</li></ul>");
                                        this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                        smooth = false;
                                    }
                                    else
                                    {
                                        bool AuthorizedNeeded = false; // default, assume no need to
                                        Authoriser NextAuthorizer = this.GetAuthorizer(ref AuthorizedNeeded);
                                        ResultObj.Status = GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize;

                                        if (AuthorizedNeeded)
                                        {
                                            // authorization required                                       
                                            if (NextAuthorizer != null)
                                            {
                                                ResultObj.NextAuthorizer = NextAuthorizer;
                                            }
                                            smooth = CurClaimManager.UpdateWhenStatusChangeToPendingAuthorizer(ResultObj);
                                            if (!smooth)
                                            {
                                                ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                                this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToPendingAuthorizer(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                            }
                                            else
                                            {
                                                if (CheckBox_email.Checked)
                                                {
                                                    if (Claimant.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                    {
                                                        #region Send email

                                                        if (GonzalesGeneralVariables.EmailActive)
                                                        {
                                                            string MailContent = "", MailTitle = "";

                                                   

                                                            #region If the claim need to go to authorizer

                                                            if (this.ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize)
                                                            {
                                                                SystemUser Receiver = curUserManager.GetUserByInternalID(this.ResultObj.NextAuthorizer.InternalID);

                                                                if (Receiver != null && Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                                {
                                                                    using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimPendingAuthorize, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                                    {
                                                                        MailContent = CurMailGenerator.GeneratePendingForAuthorizerClaimForAuthorizer(
                                                                            this.ResultObj.ClaimedBy.DisplayName,
                                                                            this.ResultObj.ClaimedBy.EmployeeID,
                                                                            this.ResultObj.ClaimedBy.EmailID,
                                                                           comp.CompanyName,
                                                                            this.ResultObj.ClaimID,
                                                                            this.ResultObj.Reasons,
                                                                            this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                            this.ResultObj.SubmittedOn.ToLongDateString(),
                                                                            GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.PENDINGAUTH_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID);

                                                                        MailTitle = CurMailGenerator.MailTitle;
                                                                    }

                                                                    if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                                    {
                                                                        using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                            Receiver.DisplayName,
                                                                            Receiver.Email,
                                                                            MailTitle,
                                                                            MailContent))
                                                                        {
                                                                            if (!CurEmailFactory.SendNow())
                                                                            {
                                                                                string LogID = SwissArmy.UniqueID();
                                                                                this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Approve_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            #endregion
                                                        }

                                                        #endregion
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            // no authorizer required, so is auto
                                            // now, it is ready for finance verification
                                            smooth = false;
                                            ErrorMessageBuilder.AppendLine("<ul><li>No Authorized Needed.</li></ul>");
                                            this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                        }
                                    }
                                }

                            //}

                        }


                        #endregion

                        # region pending to FinanceVerification/Finance admin
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification)
                        {
                            smooth = false;

                              SystemUser Approver = null;
                                Approver = curUserManager.GetUserByInternalID(this.DropDownList_Personal.SelectedValue.ToString());
                                if (Approver != null)
                                {
                                    ResultObj.NextFinanceApprover = Approver;
                                }
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                smooth = CurClaimManager.UpdateWhenStatusChangeToPendingFinanceAdmin(ResultObj);
                                if (!smooth)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToPendingFinanceAdmin(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                                    //added by mei chze on 3/1/2013
                                else
                                {
                                    if (CheckBox_email.Checked)
                                    {
                                        if (this.ResultObj.ClaimedBy.EmailID.ToLower().CompareTo("brian.king") != 0)
                                        {
                                            #region Send email

                                            if (GonzalesGeneralVariables.EmailActive)
                                            {
                                                string MailContent = "", MailTitle = "";

                                                #region If the claim already approved by last approver

                                                if (this.ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification || this.ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize)
                                                {
                                                    SystemUser Receiver = null;
                                                    using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                    {
                                                        Receiver = CurUserManager.GetUserByInternalID(this.DropDownList_Personal.SelectedValue.ToString());
                                                    }
                                                    if (Receiver != null && Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                    {
                                                        using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimApproved, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                        {
                                                            MailContent = CurMailGenerator.GenerateApprovedClaimForClaimant(
                                                                 this.ResultObj.ClaimedBy.DisplayName,
                                                                 this.ResultObj.ClaimedBy.EmployeeID,
                                                                 this.ResultObj.ClaimedBy.EmailID,
                                                                 comp.CompanyName,
                                                                this.ResultObj.ClaimID,
                                                                this.ResultObj.Reasons,
                                                                this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                this.ResultObj.NextApprover.InternalID,
                                                                GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.APPROVED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + this.ResultObj.ClaimedBy.EmailID);

                                                            MailTitle = CurMailGenerator.MailTitle;
                                                        }

                                                        if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                        {
                                                            using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                Receiver.DisplayName,
                                                                Receiver.Email,
                                                                MailTitle,
                                                                MailContent))
                                                            {
                                                                if (!CurEmailFactory.SendNow())
                                                                {
                                                                    string LogID = SwissArmy.UniqueID();
                                                                    this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Approve_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                #endregion
                                            }

                                            #endregion
                                        }
                                    }
                                }
                               
                            //}

                        }
                        #endregion

                        #region pending to FinanceVirification

                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.FinanceApproval)
                        {
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.FinanceApproval)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                SystemUsers FinanceApprovers = curUserManager.GetFinanceApproversByCompany(this.ResultObj.CompanyClaimedFrom.InternalID);
                                if (FinanceApprovers.Count > 0)
                                {
                                    # region Having Finance Approver, start to work!

                                    MasterBaseCollection CurrencyCollection = null;
                                    bool FinanceApprovalNeeded = false;

                                    // diff currency, need to convert
                                    using (CurrencyManager CurCurrencyManager = new CurrencyManager(this.CurrentPrivateSession))
                                    {
                                        CurrencyCollection = CurCurrencyManager.GetLatestExchangeRate(this.ResultObj.LocalCurrency);
                                    }

                                    for (int x = 0; x < FinanceApprovers.Count; x++)
                                    {
                                        FinanceApprover CurrentFApprover = (FinanceApprover)FinanceApprovers[x];
                                        decimal MinLimitInLocal = 0, MaxLimitInLocal = 0;

                                        // convert to local currency
                                        if (this.ResultObj.LocalCurrency.InternalID.CompareTo(CurrentFApprover.LimitCurrency.InternalID) == 0)
                                        {
                                            // same to local currency 
                                            MinLimitInLocal = CurrentFApprover.MinLimit;
                                            MaxLimitInLocal = CurrentFApprover.MaxLimit;
                                        }
                                        else
                                        {
                                            // compare with foreign currency
                                            for (int y = 0; y < CurrencyCollection.Count; y++)
                                            {
                                                Currency CurCurrency = (Currency)CurrencyCollection[y];

                                                if (CurCurrency.InternalID.CompareTo(CurrentFApprover.LimitCurrency.InternalID) == 0)
                                                {
                                                    MinLimitInLocal = CurrentFApprover.MinLimit * decimal.Parse(CurCurrency.ExchangeRate.ToString());
                                                    MaxLimitInLocal = CurrentFApprover.MaxLimit * decimal.Parse(CurCurrency.ExchangeRate.ToString());
                                                    break;
                                                }
                                            }
                                        }

                                        // ready to compare now
                                        if (this.ResultObj.TotalClaimedAmount >= MinLimitInLocal && this.ResultObj.TotalClaimedAmount <= MaxLimitInLocal)
                                        {
                                            // found the authorizer
                                            FinanceApprovalNeeded = true;

                                            //select detail claim, calculate total amount 
                                            ResultObj.ClaimDetails = CurClaimManager.GetClaimItemsByClaimHeaderInLang(this.ClaimInternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage);
                                            smooth = CurClaimManager.UpdateDetailClaimStatusChangeApprovedByFinanceApprover(ResultObj);
                                            if (!smooth)
                                            {
                                                ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                                this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateDetailClaimStatusChangeApprovedByFinanceApprover(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                                break;
                                            }
                                            this.ResultObj.NextFinanceApprover = FinanceApprovers[x];
                                            this.ResultObj.Status = GonzalesClaimHeader.TypeOfClaimStatus.Verified; // change to verified
                                            break;
                                        }

                                    }

                                    // after break from the loop
                                    if (!FinanceApprovalNeeded)
                                    {
                                        // since no finance approval needed, can submit for payment now.
                                        ErrorMessageBuilder.AppendLine("<ul><li>No Finance Approval Needed.</li></ul>");
                                        this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                    }
                                    else
                                    {
                                        smooth = CurClaimManager.UpdateWhenStatusChangeToPendingforFinanceVerifier(ResultObj);
                                        if (!smooth)
                                        {
                                            ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                            this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToPendingforFinanceVerifier(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                            this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                        }
                                        else
                                        {
                                            if (CheckBox_email.Checked)
                                            {
                                                if (this.ResultObj.ClaimedBy.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                {
                                                    #region Send email

                                                    if (GonzalesGeneralVariables.EmailActive)
                                                    {
                                                        string MailContent = "", MailTitle = "";

                                                        #region If the claim already approved by last approver

                                                        if (this.ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification || this.ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize)
                                                        {
                                                            using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimApproved, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                            {
                                                                MailContent = CurMailGenerator.GenerateApprovedClaimForClaimant(
                                                                    this.ResultObj.ClaimedBy.DisplayName,
                                                                    this.ResultObj.ClaimedBy.EmployeeID,
                                                                    this.ResultObj.ClaimedBy.EmailID,
                                                                   comp.CompanyName,
                                                                    this.ResultObj.ClaimID,
                                                                    this.ResultObj.Reasons,
                                                                    this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                    this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName,
                                                                    GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.APPROVED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + this.ResultObj.ClaimedBy.EmailID);

                                                                MailTitle = CurMailGenerator.MailTitle;
                                                            }

                                                            if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                            {
                                                                using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                    this.ResultObj.ClaimedBy.DisplayName,
                                                                    this.ResultObj.ClaimedBy.Email,
                                                                    MailTitle,
                                                                    MailContent))
                                                                {
                                                                    if (!CurEmailFactory.SendNow())
                                                                    {
                                                                        string LogID = SwissArmy.UniqueID();
                                                                        this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Approve_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                    }
                                                                }
                                                            }
                                                        }

                                                        #endregion                                                      
                                                    }

                                                    #endregion
                                                }
                                            }
                                        }
                                    }

                                    #endregion
                                }
                                else
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>No Finance Approval Needed.</li></ul>");
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                            //}
                        }
                        #endregion

                        #region on hold by FinanceAdmin
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.OnHold)
                        {
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.OnHold)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                SystemUser Finance = null;
                                Finance = curUserManager.GetUserByInternalID(this.DropDownList_Personal.SelectedValue.ToString());
                                if (Finance != null)
                                {
                                    ResultObj.OnHoldBy = Finance;
                                    //set the escalate date on getdate()
                                    ResultObj.OnHoldOn = DateTime.Now;
                                }
                                smooth = CurClaimManager.UpdateWhenStatusChangeToOnHold(ResultObj);
                                if (!smooth)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToEscalated(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                            //}

                        }
                        #endregion

                        # region submit for payment
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.SubmittedForPayment)
                        {
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.SubmittedForPayment)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                ResultObj.ProcessedBy = this.CurrentPrivateSession.CurrentLoggedOnUser;
                                ResultObj.ProcessedOn = DateTime.Now;

                                //select detail claim, calculate total amount 
                                ResultObj.ClaimDetails = CurClaimManager.GetClaimItemsByClaimHeaderInLang(this.ClaimInternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage);
                                smooth = CurClaimManager.UpdateDetailClaimStatusChangeApprovedByFinanceApprover(ResultObj);
                                if (!smooth)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateDetailClaimStatusChangeApprovedByFinanceApprover(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }

                                smooth = CurClaimManager.UpdateWhenStatusChangeToSubmitForPayment(ResultObj);
                                if (!smooth)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToEscalated(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                                else
                                {
                                    if (CheckBox_email.Checked)
                                    {
                                        if (this.ResultObj.ClaimedBy.EmailID.ToLower().CompareTo("brian.king") != 0)
                                        {
                                            #region Send email

                                            if (GonzalesGeneralVariables.EmailActive)
                                            {
                                                string MailContent = "", MailTitle = "";
                                                SystemUser Receiver = null;

                                                using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                {
                                                    Receiver = CurUserManager.GetUserByInternalID(this.ResultObj.ClaimedBy.InternalID);
                                                }

                                                if (Receiver != null && Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                {
                                                    using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimVerified, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                    {
                                                        MailContent = CurMailGenerator.GenerateVerifiedClaimForClaimant(
                                                            Receiver.DisplayName,
                                                            Receiver.EmployeeID,
                                                            Receiver.EmailID,
                                                            Receiver.FromCompany.CompanyName,
                                                            this.ResultObj.ClaimID,
                                                            this.ResultObj.Reasons,
                                                            this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                            this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.FinalApprovedAmount,
                                                            GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.REJECTED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID);

                                                        MailTitle = CurMailGenerator.MailTitle;
                                                    }
                                                }

                                                if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                {
                                                    using (EmailFactory CurEmailFactory = new EmailFactory(
                                                        Receiver.DisplayName,
                                                        Receiver.Email,
                                                        MailTitle,
                                                        MailContent))
                                                    {
                                                        if (!CurEmailFactory.SendNow())
                                                        {
                                                            string LogID = SwissArmy.UniqueID();
                                                            this.CurLogManager.LogError(LogID, "[GonzalesAdminChangeClaimStatusDetail.aspx.Button_Reject_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                        }
                                                    }
                                                }
                                            }

                                            #endregion
                                        }
                                    }
                                }
                            //}
                        }
                        #endregion

                        # region reject by approver
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByApprover)
                        {
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByApprover)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                            ResultObj.Status = GonzalesClaimHeader.TypeOfClaimStatus.RejectedByApprover;
                                smooth = CurClaimManager.UpdateStatusChangeRejectByApprover(ResultObj);
                                // this is to monitor entire claim flow

                                SystemUser Approver = null;
                                Approver = curUserManager.GetUserByInternalID(ResultObj.NextApprover.InternalID);
                                if (Approver != null)
                                {
                                    this.CurLogManager.LogAction(this.ClaimInternalID, Approver.InternalID, GonzalesUserAction.ClaimRejectedByApprover, GonzalesClaimHeader.TypeOfClaimStatus.RejectedByApprover);

                                    //Added By Agnes [16th Aug, 2011]
                                    int count = CurLogManager.GetClaimActionLogCountByClaimID(this.ClaimInternalID);
                                    if (count == 0)
                                        CurLogManager.CreateLogApproverClaimAction(this.ClaimInternalID, Approver.InternalID, Approver.DisplayName, ClaimManager.GetClaimStatusInString(this.ResultObj.Status));
                                    else
                                        CurLogManager.UpdateLogApproverClaimAction(this.ClaimInternalID, Approver.InternalID, Approver.DisplayName, ClaimManager.GetClaimStatusInString(this.ResultObj.Status), count.ToString());

                                }                               
                                if (!smooth)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToEscalated(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                                else
                                {
                                    if (CheckBox_email.Checked)
                                    {
                                        #region Send email

                                        if (GonzalesGeneralVariables.EmailActive)
                                        {
                                            string MailContent = "", MailTitle = "";
                                            SystemUser Receiver = null;
                                            Approver = null;

                                            #region Send a mail to claimant

                                            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                            {
                                                Receiver = CurUserManager.GetUserByInternalID(this.ResultObj.ClaimedBy.InternalID);
                                            }

                                            if (Receiver != null && Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                            {
                                                using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimRejected, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                {
                                                    MailContent = CurMailGenerator.GenerateRejectedClaimForClaimant(
                                                        Receiver.DisplayName,
                                                        Receiver.EmployeeID,
                                                        Receiver.EmailID,
                                                        Receiver.FromCompany.CompanyName,
                                                        this.ResultObj.ClaimID,
                                                        this.ResultObj.Reasons,
                                                        this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                        ClaimManager.GetClaimStatusInString(this.ResultObj.Status) + " - " + this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName,
                                                        GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.REJECTED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID);

                                                    MailTitle = CurMailGenerator.MailTitle;
                                                }

                                                if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                {
                                                    using (EmailFactory CurEmailFactory = new EmailFactory(
                                                        Receiver.DisplayName,
                                                        Receiver.Email,
                                                        MailTitle,
                                                        MailContent))
                                                    {
                                                        if (!CurEmailFactory.SendNow())
                                                        {
                                                            string LogID = SwissArmy.UniqueID();
                                                            this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Reject_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                        }
                                                    }
                                                }
                                            }

                                            #endregion

                                            #region Send mails back to previous approver

                                            LogsCollection ActionLogCollection = null;
                                            ActionLogCollection = this.CurLogManager.GetActionLogByClaimID(this.ClaimInternalID);

                                            if (ActionLogCollection != null)
                                            {
                                                for (int x = 0; x < ActionLogCollection.Count; x++)
                                                {
                                                    ActionLogItem CurLogItem = (ActionLogItem)ActionLogCollection[x];

                                                    // only pick those approved and authorized
                                                    if (CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Approved ||
                                                        CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Escalated ||
                                                        CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Authorized ||
                                                        CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize ||
                                                        CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification)
                                                    {
                                                        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                        {
                                                            Approver = CurUserManager.GetUserByInternalID(CurLogItem.LoggedBy.InternalID);

                                                            if (Approver != null && Approver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                            {
                                                                using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimRejectedNotifyApprover, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                                {
                                                                    MailContent = CurMailGenerator.GenerateRejectedClaimForPreviousApprover(
                                                                        Receiver.DisplayName,
                                                                        Receiver.EmployeeID,
                                                                        Receiver.EmailID,
                                                                        Receiver.FromCompany.CompanyName,
                                                                        this.ResultObj.ClaimID,
                                                                        this.ResultObj.Reasons,
                                                                        this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                        ClaimManager.GetClaimStatusInString(this.ResultObj.Status) + " - " + Approver.DisplayName,
                                                                        GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.REJECTEDNOTIFYAPPR_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID,
                                                                        CurLogItem.LogDate.ToLongDateString());

                                                                    MailTitle = CurMailGenerator.MailTitle;
                                                                }

                                                                if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                                {
                                                                    using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                        Approver.DisplayName,
                                                                        Approver.Email,
                                                                        MailTitle,
                                                                        MailContent))
                                                                    {
                                                                        if (!CurEmailFactory.SendNow())
                                                                        {
                                                                            string LogID = SwissArmy.UniqueID();
                                                                            this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Reject_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                string LogID = SwissArmy.UniqueID();
                                                                this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Reject_Click] : Failed at CurUserManager.GetUserByInternalID('" + CurLogItem.LoggedBy.InternalID + "') : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                            }   // if receiver != null
                                                        } // user manager
                                                    }


                                                }
                                            }

                                            #endregion
                                        }

                                        #endregion
                                    }
                                }
                            //}
                        }
                        #endregion

                        # region reject by authorizer
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByAuthorizer)
                        {
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByAuthorizer)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                SystemUser Claimant = null;
                                Claimant = curUserManager.GetUserByInternalID(this.HiddenField_personalID.Value.ToString());
                                if (Claimant.NoAuthorizer)
                                {
                                    // no authorizer required, so is auto
                                    // now, it is ready for finance verification
                                    ErrorMessageBuilder.AppendLine("<ul><li>No Authorized Needed.</li></ul>");
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                    smooth = false;
                                }
                                else
                                {
                                    bool AuthorizedNeeded = false; // default, assume no need to
                                    Authoriser NextAuthorizer = this.GetAuthorizer(ref AuthorizedNeeded);
                                    ResultObj.Status = GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize;

                                    if (AuthorizedNeeded)
                                    {
                                        // authorization required                                       
                                        if (NextAuthorizer != null)
                                        {
                                            ResultObj.NextAuthorizer = NextAuthorizer;
                                        }
                                        smooth = CurClaimManager.UpdateStatusChangeRejectByAuthorized(ResultObj);
                                        if (!smooth)
                                        {
                                            ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                            this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToPendingAuthorizer(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                            this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                        }
                                        else
                                        {
                                            if (CheckBox_email.Checked)
                                            {
                                                #region Send email
                                                if (this.ResultObj.ClaimedBy.DisplayName.CompareTo("Brian Douglas King") != 0)
                                                {
                                                    if (GonzalesGeneralVariables.EmailActive)
                                                    {
                                                        string MailContent = "", MailTitle = "";
                                                        SystemUser Receiver = null;
                                                        SystemUser Authorizer = null;

                                                        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                        {
                                                            Receiver = CurUserManager.GetUserByInternalID(this.ResultObj.ClaimedBy.InternalID);
                                                        }

                                                        #region Send email back to claimant
                                                        if (Receiver != null && Receiver.DisplayName.CompareTo("Brian Douglas King") != 0)
                                                        {
                                                            using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimRejected, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                            {
                                                                MailContent = CurMailGenerator.GenerateRejectedClaimForClaimant(
                                                                    Receiver.DisplayName,
                                                                    Receiver.EmployeeID,
                                                                    Receiver.EmailID,
                                                                    Receiver.FromCompany.CompanyName,
                                                                    this.ResultObj.ClaimID,
                                                                    this.ResultObj.Reasons,
                                                                    this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                    ClaimManager.GetClaimStatusInString(this.ResultObj.Status) + " - " + this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName,
                                                                    GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.REJECTED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID);

                                                                MailTitle = CurMailGenerator.MailTitle;
                                                            }
                                                        }

                                                        if (MailContent != null)
                                                        {
                                                            using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                Receiver.DisplayName,
                                                                Receiver.Email,
                                                                MailTitle,
                                                                MailContent))
                                                            {
                                                                if (!CurEmailFactory.SendNow())
                                                                {
                                                                    string LogID = SwissArmy.UniqueID();
                                                                    this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForAuthorizedDetail.Button_Reject_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                }
                                                            }
                                                        }
                                                        #endregion

                                                        #region Send mails back to previous approver

                                                        LogsCollection ActionLogCollection = null;
                                                        ActionLogCollection = this.CurLogManager.GetActionLogByClaimID(this.ClaimInternalID);

                                                        if (ActionLogCollection != null)
                                                        {
                                                            for (int x = 0; x < ActionLogCollection.Count; x++)
                                                            {
                                                                ActionLogItem CurLogItem = (ActionLogItem)ActionLogCollection[x];

                                                                // only pick those approved and authorized
                                                                if (CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Approved ||
                                                                    CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Escalated ||
                                                                    CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Authorized ||
                                                                    CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize ||
                                                                    CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification)
                                                                {
                                                                    using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                                    {
                                                                        Authorizer = CurUserManager.GetUserByInternalID(CurLogItem.LoggedBy.InternalID);

                                                                        if (Authorizer != null && Authorizer.DisplayName.CompareTo("Brian Douglas King") != 0)
                                                                        {
                                                                            using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimRejectedNotifyApprover, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                                            {
                                                                                MailContent = CurMailGenerator.GenerateRejectedClaimForPreviousApprover(
                                                                                    Receiver.DisplayName,
                                                                                    Receiver.EmployeeID,
                                                                                    Receiver.EmailID,
                                                                                    Receiver.FromCompany.CompanyName,
                                                                                    this.ResultObj.ClaimID,
                                                                                    this.ResultObj.Reasons,
                                                                                    this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                                    ClaimManager.GetClaimStatusInString(this.ResultObj.Status) + " - " + Authorizer.DisplayName,
                                                                                    GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.REJECTEDNOTIFYAPPR_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID,
                                                                                    CurLogItem.LogDate.ToLongDateString());

                                                                                MailTitle = CurMailGenerator.MailTitle;
                                                                            }

                                                                            if (MailContent != null)
                                                                            {
                                                                                using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                                    Authorizer.DisplayName,
                                                                                    Authorizer.Email,
                                                                                    MailTitle,
                                                                                    MailContent))
                                                                                {
                                                                                    if (!CurEmailFactory.SendNow())
                                                                                    {
                                                                                        string LogID = SwissArmy.UniqueID();
                                                                                        this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Reject_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            string LogID = SwissArmy.UniqueID();
                                                                            this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Reject_Click] : Failed at CurUserManager.GetUserByInternalID('" + CurLogItem.LoggedBy.InternalID + "') : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                        }   // if receiver != null
                                                                    } // user manager
                                                                }


                                                            }
                                                        }

                                                        #endregion
                                                    }
                                                }

                                                #endregion
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // no authorizer required, so is auto
                                        // now, it is ready for finance verification
                                        ErrorMessageBuilder.AppendLine("<ul><li>No Authorized Needed.</li></ul>");
                                        this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                    }
                                }
                                if (smooth)
                                {
                                    //this.CurLogManager.LogAction(this.ClaimInternalID, this.CurrentPrivateSession.ActualLoginUser.InternalID, GonzalesUserAction.ClaimStatusChange, ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()));
                                    // this is to monitor entire claim flow
                                    this.CurLogManager.LogAction(this.ClaimInternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID, GonzalesUserAction.ClaimRejectedByAuthorizer, this.ResultObj.Status);

                                    //Added By Agnes [16th Aug, 2011]
                                    CurLogManager.UpdateLogAuthorizerClaimAction(this.ClaimInternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName, ClaimManager.GetClaimStatusInString(this.ResultObj.Status));

                                    ErrorMessageBuilder.AppendLine("<ul><li>Status change from " + this.HiddenField_originStatus.Value + " To " + DropDownList_Status.SelectedItem.ToString() + " </li></ul>");
                                    this.ShowMessage(ErrorMessageBuilder.ToString());
                                }
                                
                            //}
                        }
                        #endregion

                        # region reject by finance admin
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance)
                        {
                            smooth = false;
                           // this.ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                SystemUser Finance = null;
                                Finance = curUserManager.GetUserByInternalID(this.DropDownList_Personal.SelectedValue.ToString());
                                if (Finance != null)
                                {
                                    ResultObj.FinanceApprovedBy = Finance;
                                }
                                smooth = CurClaimManager.UpdateStatusChangeRejectByFinance(ResultObj);
                                this.CurLogManager.LogAction(this.ClaimInternalID, Finance.InternalID, GonzalesUserAction.ClaimRejectedByFinanceAdmin, GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance);

                             //Added By Agnes [16th Aug, 2011]
                                CurLogManager.UpdateLogFAClaimAction(this.ClaimInternalID, Finance.InternalID, Finance.DisplayName, ClaimManager.GetClaimStatusInString(this.ResultObj.Status));
                    
                                if (!smooth)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToEscalated(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                                else
                                {
                                    if (CheckBox_email.Checked)
                                    {
                                        if (this.ResultObj.ClaimedBy.EmailID.ToLower().CompareTo("brian.king") != 0)
                                        {
                                            #region Send email

                                            if (GonzalesGeneralVariables.EmailActive)
                                            {
                                                string MailContent = "", MailTitle = "";
                                                SystemUser Receiver = null;
                                                SystemUser FinanceAdmin = null;

                                                using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                {
                                                    Receiver = CurUserManager.GetUserByInternalID(this.ResultObj.ClaimedBy.InternalID);
                                                }

                                                #region Send email back to claimant
                                                if (Receiver != null && Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                {
                                                    using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimRejected, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                    {
                                                        MailContent = CurMailGenerator.GenerateRejectedClaimForClaimant(
                                                            Receiver.DisplayName,
                                                            Receiver.EmployeeID,
                                                            Receiver.EmailID,
                                                            Receiver.FromCompany.CompanyName,
                                                            this.ResultObj.ClaimID,
                                                            this.ResultObj.Reasons,
                                                            this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                            ClaimManager.GetClaimStatusInString(this.ResultObj.Status) + " - " + this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName,
                                                            GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.REJECTED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID);

                                                        MailTitle = CurMailGenerator.MailTitle;
                                                    }
                                                }

                                                if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                {
                                                    using (EmailFactory CurEmailFactory = new EmailFactory(
                                                        Receiver.DisplayName,
                                                        Receiver.Email,
                                                        MailTitle,
                                                        MailContent))
                                                    {
                                                        if (!CurEmailFactory.SendNow())
                                                        {
                                                            string LogID = SwissArmy.UniqueID();
                                                            this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForVerificationDetail.Button_Reject_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                        }
                                                    }
                                                }
                                                #endregion

                                                #region Send mails back to previous approver

                                                LogsCollection ActionLogCollection = null;
                                                ActionLogCollection = this.CurLogManager.GetActionLogByClaimID(this.ClaimInternalID);

                                                if (ActionLogCollection != null)
                                                {
                                                    for (int x = 0; x < ActionLogCollection.Count; x++)
                                                    {
                                                        ActionLogItem CurLogItem = (ActionLogItem)ActionLogCollection[x];

                                                        // only pick those approved and authorized
                                                        if (CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Approved ||
                                                            CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Escalated ||
                                                            CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.Authorized ||
                                                            CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize ||
                                                            CurLogItem.ActionStatus == GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification)
                                                        {
                                                            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                            {
                                                                FinanceAdmin = CurUserManager.GetUserByInternalID(CurLogItem.LoggedBy.InternalID);

                                                                if (FinanceAdmin != null && FinanceAdmin.DisplayName.CompareTo("Brian Douglas King") != 0)
                                                                {
                                                                    using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + EmailTemplate.ClaimRejectedNotifyApprover, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                                    {
                                                                        MailContent = CurMailGenerator.GenerateRejectedClaimForPreviousApprover(
                                                                            Receiver.DisplayName,
                                                                            Receiver.EmployeeID,
                                                                            Receiver.EmailID,
                                                                            Receiver.FromCompany.CompanyName,
                                                                            this.ResultObj.ClaimID,
                                                                            this.ResultObj.Reasons,
                                                                            this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                                            ClaimManager.GetClaimStatusInString(this.ResultObj.Status) + FinanceAdmin.DisplayName,
                                                                            GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.REJECTEDNOTIFYAPPR_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID,
                                                                            CurLogItem.LogDate.ToLongDateString());

                                                                        MailTitle = CurMailGenerator.MailTitle;
                                                                    }

                                                                    if (MailContent != null)
                                                                    {
                                                                        using (EmailFactory CurEmailFactory = new EmailFactory(
                                                                            FinanceAdmin.DisplayName,
                                                                            FinanceAdmin.Email,
                                                                            MailTitle,
                                                                            MailContent))
                                                                        {
                                                                            if (!CurEmailFactory.SendNow())
                                                                            {
                                                                                string LogID = SwissArmy.UniqueID();
                                                                                this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Reject_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    string LogID = SwissArmy.UniqueID();
                                                                    this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Reject_Click] : Failed at CurUserManager.GetUserByInternalID('" + CurLogItem.LoggedBy.InternalID + "') : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                                }   // if receiver != null
                                                            } // user manager
                                                        }


                                                    }
                                                }

                                                #endregion
                                            }

                                            #endregion
                                        }
                                    }
                                }

                            //}
                        }
                        #endregion

                        # region reject by finance virifier
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinanceApprover)
                        {
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinanceApprover)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                                using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                {

                                    SystemUsers FinanceApprovers = CurUserManager.GetFinanceApproversByCompany(this.ResultObj.CompanyClaimedFrom.InternalID);
                                    if (FinanceApprovers.Count > 0)
                                    {
                                        # region Having Finance Approver, start to work!

                                        MasterBaseCollection CurrencyCollection = null;
                                        bool FinanceApprovalNeeded = false;

                                        // diff currency, need to convert
                                        using (CurrencyManager CurCurrencyManager = new CurrencyManager(this.CurrentPrivateSession))
                                        {
                                            CurrencyCollection = CurCurrencyManager.GetLatestExchangeRate(this.ResultObj.LocalCurrency);
                                        }

                                        for (int x = 0; x < FinanceApprovers.Count; x++)
                                        {
                                            FinanceApprover CurrentFApprover = (FinanceApprover)FinanceApprovers[x];
                                            decimal MinLimitInLocal = 0, MaxLimitInLocal = 0;

                                            // convert to local currency
                                            if (this.ResultObj.LocalCurrency.InternalID.CompareTo(CurrentFApprover.LimitCurrency.InternalID) == 0)
                                            {
                                                // same to local currency 
                                                MinLimitInLocal = CurrentFApprover.MinLimit;
                                                MaxLimitInLocal = CurrentFApprover.MaxLimit;
                                            }
                                            else
                                            {
                                                // compare with foreign currency
                                                for (int y = 0; y < CurrencyCollection.Count; y++)
                                                {
                                                    Currency CurCurrency = (Currency)CurrencyCollection[y];

                                                    if (CurCurrency.InternalID.CompareTo(CurrentFApprover.LimitCurrency.InternalID) == 0)
                                                    {
                                                        MinLimitInLocal = CurrentFApprover.MinLimit * decimal.Parse(CurCurrency.ExchangeRate.ToString());
                                                        MaxLimitInLocal = CurrentFApprover.MaxLimit * decimal.Parse(CurCurrency.ExchangeRate.ToString());
                                                        break;
                                                    }
                                                }
                                            }

                                            // ready to compare now
                                            if (this.ResultObj.TotalClaimedAmount >= MinLimitInLocal && this.ResultObj.TotalClaimedAmount <= MaxLimitInLocal)
                                            {
                                                // found the authorizer
                                                FinanceApprovalNeeded = true;
                                                this.ResultObj.NextFinanceApprover = FinanceApprovers[x];
                                                smooth = CurClaimManager.UpdateStatusChangeRejectByFinance(ResultObj);
                                                // this is to monitor entire claim flow
                                                this.CurLogManager.LogAction(this.ClaimInternalID, FinanceApprovers[x].InternalID, GonzalesUserAction.ClaimRejectedByFinanceAapprover, this.ResultObj.Status);

                                                //Added By Agnes [16th Aug, 2011]
                                                CurLogManager.UpdateLogFVClaimAction(this.ClaimInternalID, FinanceApprovers[x].InternalID, FinanceApprovers[x].DisplayName, ClaimManager.GetClaimStatusInString(this.ResultObj.Status));

                                                if (!smooth)
                                                {
                                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToEscalated(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                                }
                                                break;
                                            }
                                        }

                                        // after break from the loop
                                        if (!FinanceApprovalNeeded)
                                        {
                                            // since no finance approval needed, can submit for payment now.
                                            ErrorMessageBuilder.AppendLine("<ul><li>No Finance Approval needed.</li></ul>");
                                            this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                        }
                                        else
                                        {
                                            if (CheckBox_email.Checked)
                                            {
                                               
                                            }
                                        }

                                        #endregion
                                    }

                                    else if (FinanceApprovers == null)
                                    {
                                        string LogID = SwissArmy.UniqueID();
                                        this.CurLogManager.LogError(LogID, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                        this.ShowErrorMessage("Error! " + LogID);
                                    }
                                }
                            //}
                        }
                        #endregion

                        # region cancelled
                        else if (ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()) == GonzalesClaimHeader.TypeOfClaimStatus.Cancelled)
                        {
                            smooth = false;
                            //if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.Cancelled)
                            //{
                            //    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessageSameStatus + "</li></ul>");
                            //    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                            //}
                            //else
                            //{
                               
                                smooth = CurClaimManager.UpdateClaimHeaderWhenCancelled (ResultObj);
                                if (!smooth)
                                {
                                    ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                                    this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at  CurClaimManager.UpdateWhenStatusChangeToEscalated(ResultObj): ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                    this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                                }
                                if (CheckBox_email.Checked)
                                {
                                    if (this.ResultObj.ClaimedBy.EmailID.ToLower().CompareTo("brian.king") != 0)
                                    {
                                        #region Send email

                                        if (GonzalesGeneralVariables.EmailActive)
                                        {
                                            string MailContent = "", MailTitle = "";

                                            #region send to approver

                                                SystemUser Receiver = null;
                                                using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                {
                                                    Receiver = CurUserManager.GetUserByInternalID(this.ResultObj.NextApprover.InternalID);
                                                }
                                                if (Receiver != null && Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                {
                                                    using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + "ClaimCancel", this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                    {
                                                        MailContent = CurMailGenerator.GeneratePendingForAuthorizerClaimForAuthorizer(
                                                            this.ResultObj.ClaimedBy.DisplayName,
                                                            this.ResultObj.ClaimedBy.EmployeeID,
                                                            this.ResultObj.ClaimedBy.EmailID,
                                                            comp.CompanyName,
                                                            this.ResultObj.ClaimID,
                                                            this.ResultObj.Reasons,
                                                            this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                            this.ResultObj.SubmittedOn.ToLongDateString(),
                                                            GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.APPROVED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID);

                                                        MailTitle = CurMailGenerator.MailTitle;
                                                    }

                                                    if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                    {
                                                        using (EmailFactory CurEmailFactory = new EmailFactory(
                                                            Receiver.DisplayName,
                                                            Receiver.Email,
                                                            MailTitle,
                                                            MailContent))
                                                        {
                                                            if (!CurEmailFactory.SendNow())
                                                            {
                                                                string LogID = SwissArmy.UniqueID();
                                                                this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Approve_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                            }
                                                        }
                                                    }
                                                }


                                            #endregion

                                            #region send to claiment

                                                 Receiver = null;
                                                using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                                                {
                                                    Receiver = CurUserManager.GetUserByInternalID(this.ResultObj.ClaimedBy.InternalID );
                                                }
                                                if (Receiver != null && Receiver.EmailID.ToLower().CompareTo("brian.king") != 0)
                                                {
                                                    using (EmailGenerator CurMailGenerator = new EmailGenerator(this.Server.MapPath(EmailTemplate.path) + "ClaimCancel", this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
                                                    {
                                                        MailContent = CurMailGenerator.GeneratePendingForAuthorizerClaimForAuthorizer(
                                                            this.ResultObj.ClaimedBy.DisplayName,
                                                            this.ResultObj.ClaimedBy.EmployeeID,
                                                            this.ResultObj.ClaimedBy.EmailID,
                                                            comp.CompanyName,
                                                            this.ResultObj.ClaimID,
                                                            this.ResultObj.Reasons,
                                                            this.ResultObj.LocalCurrency.InternalID + " " + this.ResultObj.TotalClaimedAmount,
                                                            this.ResultObj.SubmittedOn.ToLongDateString(),
                                                            GonzalesGeneralVariables.URL + "Default.aspx?redirect=" + ActionRedirect.SUBMITTED_CLAIM + "&id=" + this.ResultObj.InternalID + "&ad=" + Receiver.EmailID);

                                                        MailTitle = CurMailGenerator.MailTitle;
                                                    }

                                                    if (MailContent != null && MailContent.Length > 0 && MailTitle.Length > 0)
                                                    {
                                                        using (EmailFactory CurEmailFactory = new EmailFactory(
                                                            Receiver.DisplayName,
                                                            Receiver.Email,
                                                            MailTitle,
                                                            MailContent))
                                                        {
                                                            if (!CurEmailFactory.SendNow())
                                                            {
                                                                string LogID = SwissArmy.UniqueID();
                                                                this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.Button_Approve_Click] : Failed at CurEmailFactory.SendNow() : " + CurEmailFactory.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                                                            }
                                                        }
                                                    }
                                                }


                                                #endregion
                                        }

                                        #endregion
                                    }
                                }
                            //}
                        }
                        #endregion

                        #endregion
                    }
                }
                if (smooth)
                {
                  

                   // this.CurLogManager.LogAction(this.ClaimInternalID, this.CurrentPrivateSession.ActualLoginUser.InternalID, GonzalesUserAction.ClaimStatusChange, ClaimManager.GetClaimStatusFromCode(DropDownList_Status.SelectedValue.ToString()));
                    ErrorMessageBuilder.AppendLine("<ul><li>Status change from " + this.HiddenField_originStatus.Value + " To " + DropDownList_Status.SelectedItem.ToString() + " </li></ul>");
                    this.ShowMessage(ErrorMessageBuilder.ToString());
                    smooth = curChangeManager.createChangeClaimStatusDetail(changeDetail);
                    if (!smooth)
                    {
                        ErrorMessageBuilder.AppendLine("<ul><li>" + this.SavedFailedMessage + "</li></ul>");
                        this.CurLogManager.LogError("[GonzalesAdminChangeClaimStatusDetail.Button_Submit_Click] : Failed at curChangeManager.createChangeClaimStatusDetail() : " + curChangeManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                        this.ShowErrorMessage(ErrorMessageBuilder.ToString());
                    }
                }
            }


         
            this.LoadData();
            this.LoadHistory();

            #region binddata
            if (this.ResultTable != null)
            {             
                this.BindData(this.ResultView);
            }
            if (this.ResultHistory != null)
            {
                this.BindDataHistory(this.ResultViewHistory);
                this.GridView_history.Visible = true;
            }
# endregion
        }
        else
        {            
            this.ShowErrorMessage(ErrorMessageBuilder.ToString());
        }
    }   
     #region OnRowCreated
    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            for (int i = 0; i < this.GridViewHeader.Count; i++)
            {
                cells[i].Text = this.GridViewHeader[i].ToString();
            }
        }
    }
    #endregion

    protected void DropDownList_Status_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList_Status.SelectedValue == ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.Escalated).ToString())
        {
            Label_Personal.Text = "Approver:";
            this.DropDownList_Personal.Visible = true;
            //find matrix approver
            this.DropDownList_Personal.Items.Clear();
            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
            {
                SystemUsers Approvers = CurUserManager.GetExceptionalApproversMatrix(this.HiddenField_personalID.Value);
                if (Approvers != null && Approvers.Count > 0)
                {
                    for (int x = 0; x < Approvers.Count; x++)
                    {
                        this.DropDownList_Personal.Items.Add(new ListItem(Approvers[x].DisplayName, Approvers[x].InternalID));
                    }
                    this.Button_Submit.Enabled = true;
                }
                else
                {
                    this.Button_Submit.Enabled = false;
                }
            }
        }
        else if (DropDownList_Status.SelectedValue == ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.OnHold).ToString() 
            || DropDownList_Status.SelectedValue == ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance).ToString()
            || DropDownList_Status.SelectedValue == ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification).ToString())
        {
            Label_Personal.Text = "Finance Approver:";
            this.DropDownList_Personal.Visible = true;
            //find finance approver
            this.DropDownList_Personal.Items.Clear();
            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
            {
                SystemUser currentclaimant = CurUserManager.GetUserByInternalID(this.HiddenField_personalID.Value);
                SystemUsers FinanceApprovers = CurUserManager.GetFinanceAdminByCompany(currentclaimant.FromCompany.InternalID);
                if (FinanceApprovers.Count > 0)
                {
                    for (int x = 0; x < FinanceApprovers.Count; x++)
                    {
                        this.DropDownList_Personal.Items.Add(new ListItem(FinanceApprovers[x].DisplayName, FinanceApprovers[x].InternalID));
                    }
                    this.Button_Submit.Enabled = true;
                }
                else
                {
                    this.Button_Submit.Enabled = false;
                }
            }
        }
        //else if (DropDownList_Status.SelectedValue == ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.FinanceApproval  ).ToString() || DropDownList_Status.SelectedValue == ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinanceApprover ).ToString())
        //{
        //    Label_Personal.Text = "Finance Approver:";
        //    this.DropDownList_Personal.Visible = true;
        //    //find finance approver
        //    this.DropDownList_Personal.Items.Clear();
        //    using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
        //    {
        //        SystemUser currentclaimant = CurUserManager.GetUserByInternalID(this.HiddenField_personalID.Value);
        //        SystemUsers FinanceApprovers = CurUserManager.GetFinanceApproversByCompany(currentclaimant.FromCompany.InternalID);
        //        if (FinanceApprovers.Count > 0)
        //        {
        //            for (int x = 0; x < FinanceApprovers.Count; x++)
        //            {
        //                this.DropDownList_Personal.Items.Add(new ListItem(FinanceApprovers[x].DisplayName, FinanceApprovers[x].InternalID));
        //            }
        //            this.Button_Submit.Enabled = true;
        //        }
        //        else
        //        {
        //            this.Button_Submit.Enabled = false;
        //        }
        //    }
        //}
        else
        {
            Label_Personal.Text = "";
            this.DropDownList_Personal.Visible = false;
            this.DropDownList_Personal.Items.Clear();
            this.Button_Submit.Enabled = true;
        }
    }

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        // browse button
        this.Button_Submit.Attributes["onclick"] = "return confirm('Are you sure change to this status?');";     
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_Status.Text = CurLanguageFactory.DisplayValue("Status");              
                this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Submit");
                this.UnexpectedError = CurLanguageFactory.DisplayValue("UnexpectedMessage");
                this.RemarkMessage = CurLanguageFactory.DisplayValue("RemarkMessage");
                this.Label_Remark.Text = CurLanguageFactory.DisplayValue("Remark");
                this.StatusMessage = CurLanguageFactory.DisplayValue("StatusMessage");
                this.SavedFailedMessage = CurLanguageFactory.DisplayValue("SaveFailedMessage");
                this.SavedFailedMessageSameStatus = CurLanguageFactory.DisplayValue("SavedFailedMessageSameStatus");

                this.GridViewHeader = new ArrayList();
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ClaimID2"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Category2"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Purpose"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("SubmittedDate"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("LocalCurrency"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AmountClaimed"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("FinalAmount"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("LessCardsTransaction"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("OwningToFromEmployee"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ClaimedBy2"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("EmployeeID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("CostCenter"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Status2"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("LastProcessedDate"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("LastProcessedBy"));

                // Status
                this.DropDownList_Status.Items.Add(new ListItem("", ""));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("Cancelled"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.Cancelled).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("PendingforApprover"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.Submitted).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("PendingforHigherApprover"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.Escalated).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("PendingforAuthorizer"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("PendingforFinanceAdmin"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("PendingforFinanceVerifier"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.FinanceApproval).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("OnHoldByFinanceAdmin"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.OnHold).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("SubmittedForPayment"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.SubmittedForPayment).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("RejectedByApprover"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.RejectedByApprover).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("RejectedByAuthorizer"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.RejectedByAuthorizer).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("RejectedByFinanceAdmin"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance).ToString()));
                this.DropDownList_Status.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("RejectedByFinanceVerifier"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinanceApprover).ToString()));
                                      
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesReportGeneralClaimants.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "ErrorBox";
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (ReportsGenerator CurReportGenerator = new ReportsGenerator(this.CurrentPrivateSession))
        {
            if ((this.ResultTable = CurReportGenerator.GetClaimHeadersByInternalID(this.ClaimInternalID)) != null)
            {
                this.ResultView = new DataView(this.ResultTable);
                this.HiddenField_originStatus.Value = this.ResultTable.Rows[0]["Status"].ToString() ;
                using (UserManager curUserManager = new UserManager(this.CurrentPrivateSession))
                {
                    string[] split = this.ResultTable.Rows[0]["ClaimedBy"].ToString().Split('(');
                    split = split[1].Split(')');
                    string id = split[0];
                    SystemUser sy = new SystemUser();
                    sy= curUserManager.GetUserByEmailID(id,true);
                    this.HiddenField_personalID.Value = sy.InternalID;
                }
               // this.HiddenField_personalID.Value = this.ResultTable.Rows[0]["ClaimedBy"].ToString();
            }
            else
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesReportGeneralClaimants.LoadData] : Failed at CurClaimManager.GetClaimByInternalIDDataTable(" +this.ClaimInternalID +")",this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage(this.UnexpectedError + LogID);
            }
        }
    }

    protected void LoadHistory()
    {
        using (ChangeManager curChangeManager = new ChangeManager(this.CurrentPrivateSession))
        {

            this.ResultHistory = curChangeManager.GetChangeStatusHistory(this.ClaimInternalID);
            this.ResultViewHistory = new DataView(ResultHistory);

        }
    }
    #endregion 


    #region BindData

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();

    }
    private void BindDataHistory( DataView resultHistory)
    {
        this.GridView_history.DataSource = resultHistory;
        this.GridView_history.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindDataHistory(DataView resultHistory, int pageindexHistory)
    {

        this.GridView_history.PageIndex = pageindexHistory;
        this.GridView_history.DataSource = resultHistory;
        this.GridView_history.DataBind();
    }
    #endregion

    public static int GetClaimStatusInCode(string status)
    {
        int result = 0;

        switch (status)
        {
                                      
            case "New":
                result =0;
                break;

            case "Draft":
               result =1;
                break;

            case "PendingforApprover":
               result = 2;
                break;

            case "PendingforHigherApprover":
                result = 3;
                break;

            case "Approved":
                result = 4;
                break;

            case "Authorizer":
                result = 5;
                break;

            case "PendingforFinanceAdmin":
                result = 6;
                break;

            case "SubmittedForPayment":
                result = 7;
                break;

            case "PendingforAuthorizer":
                result = 8;
                break;

            case "Verified":
                result = 97;
                break;

            case "Processed":
                result = 98;
                break;

            case "FinanceApproval":
                result = 99;
                break;

            case "Paid":
                result = 100;
                break;

            case "RejectedByApprover":
                result = 101;
                break;

            case "RejectedByAuthorizer":
                result = 102;
                break;

            case "RejectedByFinanceAdmin":
                result = 103;
                break;

            case "RejectedByFinanceVerifier":
                result = 104;
                break;

            case "RejectedByFinanceApproverToFA":
                result = 106;
                break;

            case "OnHoldByFinanceAdmin":
                result = 105;
                break;

            case "Cancelled":
                result = 200;
                break;

            case "ProcessedUpdated":
                result = 201;
                break;



        }

        return result;
    }

    #region comment
    /*public static GonzalesClaimHeader.TypeOfClaimStatus GetClaimStatusFromCode(string code)
    {
        GonzalesClaimHeader.TypeOfClaimStatus result = GonzalesClaimHeader.TypeOfClaimStatus.Null;

        switch (code)
        {
                     
            case "0":
                result = GonzalesClaimHeader.TypeOfClaimStatus.New;
                break;

            case "1":
                result = GonzalesClaimHeader.TypeOfClaimStatus.Draft;
                break;

            case "2":
                result = GonzalesClaimHeader.TypeOfClaimStatus.Cancelled;
                break;

            case "3":
                result = GonzalesClaimHeader.TypeOfClaimStatus.Submitted;
                break;

            case "4":
                result = GonzalesClaimHeader.TypeOfClaimStatus.Escalated;
                break;

            case "5":
                result = GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize;
                break;  

            case "6":
                result = GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification;
                break;

            case "7":
                result = GonzalesClaimHeader.TypeOfClaimStatus.FinanceApproval;
                break;

            case "8":
                result = GonzalesClaimHeader.TypeOfClaimStatus.OnHold;
                break;

            case "9":
                result = GonzalesClaimHeader.TypeOfClaimStatus.SubmittedForPayment;
                break;     
          
            case "10":
                result = GonzalesClaimHeader.TypeOfClaimStatus.RejectedByApprover;
                break;

            case "11":
                result = GonzalesClaimHeader.TypeOfClaimStatus.RejectedByAuthorizer;
                break;

            case "12":
                result = GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance;
                break;

            case "13":
                result = GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinanceApprover;
                break;     
        }

        return result;
    }
    */
    # endregion

    #region GetAuthorizer

    protected Authoriser GetAuthorizer(ref bool AuthorizedNeeded)
    {
        SystemUsers AuthorizersCollection = null;
        SystemUser Claimant = null;
        Authoriser ResultAuthorizer = null;

        #region 1. Get list of Authorizers

        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
        {
            Claimant = CurUserManager.GetUserByInternalID(this.HiddenField_personalID.Value.ToString());

            if (Claimant != null)
            {
                AuthorizersCollection = CurUserManager.GetAuthorizersByEntity(Claimant.FromCompany.Entity.InternalID);

                if (AuthorizersCollection == null)
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.GetAuthorizer] : Failed at CurUserManager.GetAuthorizersByEntity('" + Claimant.FromCompany.Entity.InternalID + "') : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage("Error! " + LogID);
                }
            }
            else
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesClaimsPendingForApprovalDetail.GetAuthorizer] : Failed at CurUserManager.GetUserByInternalID('" + this.ResultObj.ClaimedBy.InternalID + "') : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage("Error! " + LogID);
            }
        }

        #endregion

       #region 2. Check if total claim exceed the limit.

        if (AuthorizersCollection != null)
        {
            MasterBaseCollection CurrencyCollection = null;

            // diff currency, need to convert
            using (CurrencyManager CurCurrencyManager = new CurrencyManager(this.CurrentPrivateSession))
            {
                CurrencyCollection = CurCurrencyManager.GetLatestExchangeRate(this.ResultObj.LocalCurrency);
            }

            for (int x = 0; x < AuthorizersCollection.Count; x++)
            {
                Authoriser CurrentAuthorizer = (Authoriser)AuthorizersCollection[x];

                decimal MinLimitInLocal = 0, MaxLimitInLocal = 0;

                // convert to local currency
                if (this.ResultObj.LocalCurrency.InternalID.CompareTo(CurrentAuthorizer.LimitCurrency.InternalID) == 0)
                {
                    // same to local currency 
                    MinLimitInLocal = CurrentAuthorizer.MinLimit;
                    MaxLimitInLocal = CurrentAuthorizer.MaxLimit;
                }
                else
                {
                    // compare with foreign currency
                    for (int y = 0; y < CurrencyCollection.Count; y++)
                    {
                        Currency CurCurrency = (Currency)CurrencyCollection[y];

                        if (CurCurrency.InternalID.CompareTo(CurrentAuthorizer.LimitCurrency.InternalID) == 0)
                        {
                            MinLimitInLocal = CurrentAuthorizer.MinLimit * decimal.Parse(CurCurrency.ExchangeRate.ToString());
                            MaxLimitInLocal = CurrentAuthorizer.MaxLimit * decimal.Parse(CurCurrency.ExchangeRate.ToString());
                            break;
                        }
                    }
                }

                // ready to compare now
                if (this.ResultObj.TotalClaimedAmount >= MinLimitInLocal && this.ResultObj.TotalClaimedAmount <= MaxLimitInLocal)
                {
                    // found the authorizer
                    AuthorizedNeeded = true;
                    ResultAuthorizer = CurrentAuthorizer;
                    break;
                }
            }
        }

        #endregion

        return ResultAuthorizer;


    }

    #endregion

    #endregion
}
